package com.坦克大战.Tank_5;

import java.util.Vector;
@SuppressWarnings("all")
/// 敌人坦克
public class EnemyTanks extends Tank implements Runnable {

    Vector<Shoot> EnemyTanksBullets = new Vector<>();
    /// 敌人坦克子弹集合

    ///boolean isLive = true;  ///生命状态

    public EnemyTanks(int x, int y, int speed) {
        super(x, y, speed);
        setDirection(2);    ///初始化方向为下
    }


    /// 实现让敌方坦克自己移动的线程方法
    @Override
    public void run() {

        while (isLive) {   /// 如果坦克死亡就结束线程
            int direction = (int) (Math.random() * 4);  ///随机4个方向
            int move = (int) (Math.random() * 30);      ///按一个方向随机移动0~30之间的步
            switch (direction) {
                case 0:
                    for (int i = 0; i < move; i++) {
                        moveUP();
                        sleep();

                    }
                    break;
                case 1:
                    for (int i = 0; i < move; i++) {
                        moveLEFT();
                        sleep();

                    }
                    break;
                case 2:
                    for (int i = 0; i < move; i++) {
                        moveDOWN(700);
                        sleep();

                    }
                    break;
                case 3:
                    for (int i = 0; i < move; i++) {
                        moveRIGHT(1500);
                        sleep();

                    }
                    break;

            }
            /// 如果敌方坦克存活，但子弹没了就给他创建一个子弹，并启动子弹；（注意：这 if语句 在敌方坦克移动完后才判断，所以等移动完后才能发射子弹，移动方法会阻塞run线程；）
            if(EnemyTanksBullets.size() <= 3 ) {
                shootMyTank();
            }

            /// 每次转向停顿一下，符合惯性
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }

    }
    /// 睡眠方法，防止移动速度非常快
   void sleep(){
       try {
           Thread.sleep(100);
       } catch (InterruptedException e) {
           throw new RuntimeException(e);
       }
   }


    /// 如果一旦发现子弹没了，就发射新的子弹
    public void shootMyTank(){

            Shoot shoot = null;

            switch (getDirection()) {///获取坦克方向，创建坦克的子弹方向；
                case 0:
                    shoot = new Shoot(getX() + 20, getY(), 0);  ///创建子弹到集合，根据炮管方向来决定位置
                    break;
                case 1:
                    shoot = new Shoot(getX(), getY() + 20, 1);
                    break;
                case 2:
                    shoot = new Shoot(getX() + 20, getY() + 60, 2);
                    break;
                case 3:
                    shoot = new Shoot(getX() + 60, getY() + 20, 3);
                    break;
            }

            EnemyTanksBullets.add(shoot);       ///将创建的子弹添加到子弹集合中
            new Thread(shoot).start();   ///启动子弹线程，让其坐标一直按一个方向移动；
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    }

